<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="8" :sm="24" >
            <a-form-item label="Course Name" :labelCol="{ style: 'width: 100px' }">
              <j-input placeholder="" v-model="queryParam.name" style="width: 100%" ></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" >
            <a-form-item label="Category" :labelCol="{ style: 'width: 100px' }">
              <j-tree-dict v-model="queryParam.categoryCode" field= "code"  style="width: 100%" parentCode="B03" />
            </a-form-item>
          </a-col>

          <template v-if="toggleSearchStatus">
            <a-col :md="8" :sm="24" >
              <a-form-item label="Course Type" :labelCol="{ style: 'width: 100px' }">
                <j-dict-select-tag type="list" v-model="queryParam.courseType"
                                   dictCode="courseType"
                                   style="width: 100%"
                                   placeholder="" />
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="24" >
              <a-form-item label="Year" :labelCol="{ style: 'width: 100px' }">
                <j-dict-select-tag type="list" v-model="queryParam.year" dictCode="year" placeholder="" style="width: 100%"/>
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="24" >
              <a-form-item label="Grade" :labelCol="{ style: 'width: 100px' }">
                <j-dict-select-tag type="list" v-model="queryParam.grade" dictCode="grade" placeholder="" style="width: 100%" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" >
              <a-form-item label="Term" :labelCol="{ style: 'width: 100px' }">
                <j-dict-select-tag type="list" v-model="queryParam.term" dictCode="term" placeholder="" style="width: 100%"/>
              </a-form-item>
            </a-col>

            <a-col :md="16" :sm="24" >
              <a-form-item label="Teacher" :labelCol="{ style: 'width: 100px' }">

                <j-multi-select-tag
                  v-model="queryParam.teacherIds"
                  :options="teacherOptions"
                  placeholder="">
                </j-multi-select-tag>

              </a-form-item>
            </a-col>

          </template>
          <a-col :md="!toggleSearchStatus && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="toggleSearchStatus && { float: 'right', overflow: 'hidden' } || {} ">
               <a-button type="primary" @click="searchQuery" icon="search">Search</a-button>
              <a-button   @click="searchReset" icon="reload" style="margin-left: 8px">Reset</a-button>

              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? 'UP' : 'DOWN' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">New</a-button>
     <!-- <a-button type="primary" icon="download" @click="handleExportXls('course_info')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <!-- 高级查询区域 -->

      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->
<!--
 bordered
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
-->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :customRow="customRow"
        @change="handleTableChange"
        >

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">Edit</a>
                    <a-divider type="vertical" />
          <a @click="handAddStudent(record)">Add Student</a>

        </span>

      </a-table>
    </div>

    <AddCourseStudentModal ref="addForm" :course-id="courseId" @ok="addFormOk"></AddCourseStudentModal>
    <course-info-modal ref="modalForm" @ok="modalFormOk"></course-info-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CourseInfoModal from './modules/CourseInfoModal'
  import JInput from '@/components/jeecg/JInput'
  import AddCourseStudentModal from './modules/AddCourseStudentModal'
  import {getAction} from "@api/manage";

  export default {
    name: 'CourseInfoList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CourseInfoModal,
      JInput,
      AddCourseStudentModal

    },
    data () {
      return {
        description: 'course_info管理页面',
        courseId : 0,
        // 表头
        columns: [
          {
            title:'ID',
            align:"center",
            width:80,
            dataIndex: 'id'
          },
          {
            title:'Enrollment',
            align:"center",
            width:80,
            dataIndex: 'enrollment'
          },
          {
            title:'Category',
            align:"center",
            width:120,
            dataIndex: 'categoryName'
          },
          {
            title:'Course Name',
            align:"center",
            width:120,
            dataIndex: 'name',
            sorter: true
          },
          {
            title:'Course Type',
            align:"center",
            width:100,
            dataIndex: 'courseType_dictText'
          },
          {
            title:'Sub Type',
            align:"center",
            width:100,
            dataIndex: 'courseSubType_dictText'
          },
          {
            title:'Term',
            align:"center",
            width:100,
            dataIndex: 'term'
          },

          {
            title:'First Name',
            align:"center",
            width:140,
            dataIndex: 'teacherFirstName'
          },
          {
            title:'Last Name',
            align:"center",
            width:140,
            dataIndex: 'teacherLastName'
          },

          {
            title:'Hours',
            align:"center",
            width:100,
            dataIndex: 'hours'
          },
          {
            title:'Price',
            align:"center",
            width:100,
            dataIndex: 'priceFormat'
          },
          {
            title:'Grade',
            align:"center",
            width:100,
            dataIndex: 'grade'
          },
          {
            title:'Start Time',
            align:"center",
            dataIndex: 'startTime',
            width:160,
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'End Time',
            align:"center",
            width:160,
            dataIndex: 'endTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:140,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/sistersophie/courseInfo/list",
          teacherList:'/sistersophie/teacherInfo/nopage/list',
          delete: "/sistersophie/courseInfo/delete",
          deleteBatch: "/sistersophie/courseInfo/deleteBatch",
          exportXlsUrl: "/sistersophie/courseInfo/exportXls",
          importExcelUrl: "sistersophie/courseInfo/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
        teacherOptions:[{label:"123",value:"123"}],

      }
    },
    created() {
      this.getSuperFieldList();
      this.loadTeacherData();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      loadTeacherData() {
        if(!this.url.teacherList){
          this.$message.error("请设置url.teacherList属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        var params = {};//查询条件
        getAction(this.url.teacherList, params).then((res) => {
          if (res.success) {
            this.teacherOptions = res.result.map((item,index,arr)=>{
              let c = {label:item.name, value:item.id+""}
              return c;
            })
          }
        })
      },
      customRow(record, index) {
        return {
          on: {
            // 鼠标单击行
            dblclick: event => {
               this.handRowClick(record,index);
            },
          },
        }
      },
      handRowClick(record, index){
        this.handleDetail(record);
      },
      addFormOk() {
        // 新增/修改 成功时，重载列表
        this.loadData();
        //清空列表选中
        this.onClearSelected()
      },
      handAddStudent:function(record){
        this.courseId = record.id;
        this.$refs.addForm.add();
        this.$refs.addForm.title="Add Student";
        this.$refs.addForm.disableSubmit = false;
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'date',value:'startTime',text:'Start Time'})
        fieldList.push({type:'date',value:'endTime',text:'End Time'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>